package com.yugong.util;

import com.yugong.common.Const;
import com.yzk18.docs.ExcelHelpers;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 办公文件操作工具类
 *
 * @author yugong
 * @date 2024/2/11
 */
@Slf4j
@Component
public class OfficeUtils {
    @Resource
    private TimeUtils timeUtils;

    /**
     * 导出Excel表格文件
     *
     * @param fileURl      Excel表格路径
     * @param fileName     Excel文件名
     * @param fileType     Excel文件类型
     * @param columnNames  Excel表格的列名
     * @param columnValues 单元格数据
     */
    public boolean exportExcel(String fileURl, String fileName, String fileType,
                               List<String> columnNames, List<Object> columnValues) {
        Workbook workbook = ExcelHelpers.createXLSX();
        Sheet sheet = workbook.createSheet();
        // 创建列名
        int colIndex = 0;
        for (String columnName : columnNames) {
            ExcelHelpers.setCellValue(sheet, 0, colIndex, columnName);
            colIndex++;
        }
        if (fileURl == null || "".equals(fileURl)) {
            FileSystemView fileSystemView = FileSystemView.getFileSystemView();
            File desktopDir = fileSystemView.getHomeDirectory();
            fileURl = desktopDir.getAbsolutePath();
        }
        if (fileName == null || "".equals(fileName)) {
            fileName = "excelDate";
        }
        if (fileType == null || "".equals(fileType)) {
            fileType = "xlsx";
        }
        int index = 0;
        for (int rowIndex = 1; rowIndex < columnValues.size(); rowIndex++) {
            if (index == columnValues.size()) {
                break;
            }
            for (colIndex = 0; colIndex < columnNames.size(); colIndex++) {
                ExcelHelpers.setCellValue(sheet, rowIndex, colIndex, columnValues.get(index).toString());
                index++;
            }
        }
        String pathName = fileURl + "/" + fileName + "." + fileType;
        ExcelHelpers.saveToFile(workbook, new File(pathName));
        ExcelHelpers.close(workbook);
        log.info("创建Excel表格日志：");
        log.info("您的Excel表格保存路径为：" + pathName);
        log.info("您的Excel表格创建日期：" + timeUtils.setDateToStrDate(new Date(), Const.YYYY_MM_DD_HH_MM_SS));
        log.info("创建Excel表格成功");
        return true;
    }

    /**
     * 导入Excel表格文件
     *
     * @param fileName    Excel文件名
     * @param inputStream Excel文件数据流
     * @return Excel文件数据列表
     */
    public List<String> importExcel(String fileName, InputStream inputStream) {
        List<String> stringList = new ArrayList<>();
        // 读取excel文件
        Workbook workbook = ExcelHelpers.openFile(inputStream);
        // 遍历工作簿
        for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
            Sheet sheet = workbook.getSheetAt(sheetIndex);
            for (int rowIndex = sheet.getFirstRowNum(); rowIndex <= sheet.getLastRowNum(); rowIndex++) {
                Row row = sheet.getRow(rowIndex);
                if (row == null) {
                    continue;
                }
                for (int colIndex = row.getFirstCellNum(); colIndex <= row.getLastCellNum(); colIndex++) {
                    Cell cell = row.getCell(colIndex);
                    if (cell == null) {
                        continue;
                    }
                    String value = ExcelHelpers.getCellStringValue(cell);
                    System.out.print(value + " ");
                    stringList.add(value);
                }
                System.out.println();
            }
        }
        ExcelHelpers.close(workbook);
        log.info("读取Excel表格日志：");
        log.info("您的Excel表格名称为：" + fileName);
        log.info("您的Excel表格读取日期：" + timeUtils.setDateToStrDate(new Date(), Const.YYYY_MM_DD_HH_MM_SS));
        log.info("读取Excel表格成功");
        return stringList;
    }
}
